<?php
defined ( 'BASEPATH' ) or die ( 'forbidden' );
class Registmail_log_Controller extends Controller {
    private $server_list;       //大区列表
    private $wholeServerHost;    //全局地址
    private $areaList;
    private $areaAddr2ID; //大区地址到大区ID的映射

    function _init() {
        $config = get_config();
        $this->wholeServerHost = $config['host_url'];

        $this->server_list=get_server_list();
        foreach($this->server_list as $k=>$row) {
            $this->areaList[$row['SvrAddr']] = $row['SvrID']."-".$row['SvrName'][1];
            $this->areaAddr2ID[$row['SvrAddr']]=$row['SvrID'];
        }
    }

    function index() {
        $this->view->display ( "seal/registmail/index" );
    }

    function flexigrid() {
        list ( $colkey, $colsinfo, $where, $sortname, $sortorder, $offset, $rp, $page ) = $this->common->get_flexigrid_params ();

        $post_data['SessionID']="ewewqw";
        $post_data['SessionKey']="wqqew";

        $total = 0;
        foreach ($this->server_list as $svrkey=>$svrinfo) { // 遍历所有游戏大区
            $post_data['SvrID']=$svrinfo['SvrID'];

            $strResult = vpost($this->wholeServerHost.'get_regist_award_lst',3,json_encode($post_data));
            $result=json_decode($strResult,true);
            if($result['RetCode'] == 0)
            {
                foreach ($result['RegistAwardLst'] as $askey=>$value) {
                    $n['id'] = ($svrinfo['SvrID'] * 1000) + $value['ID'];
                    $n['awardid'] = $value['ID'];
                    $n['mail_title'] = $value['Title'];
                    $n['mail_content'] = $value['Msg'];
                    $n['itemid0'] = $value['ItemLst'][0]['ID'];
                    $n['amount0'] = $value['ItemLst'][0]['Num'];
                    $n['itemid1'] = $value['ItemLst'][1]['ID'];
                    $n['amount1'] = $value['ItemLst'][1]['Num'];
                    $n['itemid2'] = $value['ItemLst'][2]['ID'];
                    $n['amount2'] = $value['ItemLst'][2]['Num'];
                    $n['itemid3'] = $value['ItemLst'][3]['ID'];
                    $n['amount3'] = $value['ItemLst'][3]['Num'];
                    $n['areanum'] = $svrinfo['SvrID'];

                    if ($n['awardid'] <=0 && $n['mail_title'] == "" && $n['mail_content'] == ""){
                        continue;
                    }

                    $row[] = array (
                            'id' => $n['id'],
                            'cell' => $n 
                    );

                    $total += 1;
                }
            }
        }

        $result = array (
                'page' => $page,
                'total' => $total,
                'rows' => $row 
        );

        echo json_encode ( $result );
    }

    function add() {
        if(is_submit())
        {
            $rowdata = $_POST['row'];
            //var_dump($rowdata);

            $post_data['SessionID']="ewewqw";
            $post_data['SessionKey']="wqqew";

            $post_data['ID']=(int)$rowdata['id']; //新手注册奖励ID
            $post_data['Title']=(string)$rowdata['title'];
            $post_data['Msg'] = (string)$rowdata['msg'];

            $itemcount = 0;
            foreach ($rowdata['item'] as $k=>$v){
                $item=array("ID"=>(int)$v,"Num"=>(int)$rowdata['amount'][$k]);
                $post_data['ItemLst'][$k]=$item;
                if ($itemcount ==0){
                    $mailloginfo['itemid0'] = (int)$v;
                    $mailloginfo['amount0'] = (int)$rowdata['amount'][$k];
                }else if  ($itemcount ==1){
                    $mailloginfo['itemid1'] = (int)$v;
                    $mailloginfo['amount1'] = (int)$rowdata['amount'][$k];
                }else if  ($itemcount ==2){
                    $mailloginfo['itemid2'] = (int)$v;
                    $mailloginfo['amount2'] = (int)$rowdata['amount'][$k];
                }else if  ($itemcount ==3){
                    $mailloginfo['itemid3'] = (int)$v;
                    $mailloginfo['amount3'] = (int)$rowdata['amount'][$k];
                }

                $itemcount += 1;
            }

            $post_data['SvrID'] = (int)$this->areaAddr2ID[$rowdata['area_host']];

            $strResult = vpost($this->wholeServerHost.'set_regist_award',3,json_encode($post_data));

            $result=json_decode($strResult,true);
            //操作失败或者连接失败
            if($result['RetCode'] == 0)
            {
                insert_log(translate('MENU_SEND_OBJECT'),translate('ADD'),NULL,$rowdata);
                echo "<script>alert('".translate('C_TIPS_SUCCESS_TEXT')."')</script>";
            } else {
                //unset($rowdata);
                if ($result['RetCode'] == 1) {
                    echo "<script>alert('" . translate('ROLE_FIGHT_VALUE_ERROR') . "')</script>";
                } else if ($result['RetCode'] == 2) {
                    echo "<script>alert('" . translate('ROLE_LEVEL_ERROR') . "')</script>";
                } else if ($result['RetCode'] == 3) {
                    echo "<script>alert('" . translate('ROLE_ROLE_ID_ERROR') . "')</script>";
                }
            }

            cp_tips ("success");
        }

        $this->view->assign("arealist", $this->form->select("row[area_host]", $this->areaList, '', array('id' => 'area_host')));
        $this->view->display ( "seal/registmail/add" );
    }

    function edit($id = 0) {
        $post_data['SessionID']="ewewqw";
        $post_data['SessionKey']="wqqew";

        $awardid =((int)$id) % 1000;
        $svrid = ((int)$id) / 1000;

        if(is_submit()){
            $rowdata = $_POST['row'];
            var_dump($rowdata);

            //$post_data['ID']=(int)$rowdata['id']; //新手注册奖励ID
            $post_data['ID'] = (int)$awardid;
            $post_data['Title']=(string)$rowdata['title'];
            $post_data['Msg'] = (string)$rowdata['msg'];

            $SvrID = (int)$rowdata['svrid'];

            $itemcount = 0;
            foreach ($rowdata['item'] as $k=>$v){
                $item=array("ID"=>(int)$v,"Num"=>(int)$rowdata['amount'][$k]);
                $post_data['ItemLst'][$k]=$item;
                if ($itemcount ==0){
                    $mailloginfo['itemid0'] = (int)$v;
                    $mailloginfo['amount0'] = (int)$rowdata['amount'][$k];
                }else if  ($itemcount ==1){
                    $mailloginfo['itemid1'] = (int)$v;
                    $mailloginfo['amount1'] = (int)$rowdata['amount'][$k];
                }else if  ($itemcount ==2){
                    $mailloginfo['itemid2'] = (int)$v;
                    $mailloginfo['amount2'] = (int)$rowdata['amount'][$k];
                }else if  ($itemcount ==3){
                    $mailloginfo['itemid3'] = (int)$v;
                    $mailloginfo['amount3'] = (int)$rowdata['amount'][$k];
                }

                $itemcount += 1;
            }

            $post_data['SvrID'] = (int)$svrid;
            $strResult = vpost($this->wholeServerHost.'set_regist_award',3,json_encode($post_data));
            $result=json_decode($strResult,true);
            if($result['RetCode'] == 0){
                insert_log(translate('MENU_SEND_OBJECT'),translate('ADD'),NULL,$rowdata);
                 echo "<script>alert('".translate('C_TIPS_SUCCESS_TEXT')."')</script>";
            }

            cp_tips ("success");
        }


        $this->view->assign("svrid", (int)$svrid);
        $this->view->assign('id', $awardid);

        $post_data['SvrID'] = (int)$svrid;
        $strResult = vpost($this->wholeServerHost.'get_regist_award',3,json_encode($post_data));
        $result=json_decode($strResult,true);
        if($result['RetCode'] == 0){
            $this->view->assign('title', $result['RegistAward']['Title']);
            $this->view->assign('msg', $result['RegistAward']['Msg']);
        }

        $this->view->display ( "seal/registmail/edit" );
    }

    function del($id = 0) {

        $awardid =((int)$id) % 1000;
        $svrid = ((int)$id) / 1000;

        $post_data['SessionID']="ewewqw";
        $post_data['SessionKey']="wqqew";
        $post_data['ID'] = $awardid;

        $post_data['SvrID'] = (int)$svrid;
        $strResult = vpost($this->wholeServerHost.'del_regist_award',3,json_encode($post_data));
        $result=json_decode($strResult,true);
        if($result['RetCode'] == 0){
           echo "<script>alert('".translate('C_TIPS_SUCCESS_TEXT')."')</script>";
        }else{
            echo "<script>alert('Failed!')</script>";
        }
        
        $this->view->display ( "seal/registmail/index" );
    }
    
    function multi() {

    }
}


?>
